# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.action_chains import ActionChains
import unittest
import random
import time
from cookies import lasthopes, randomword
import re
import string
import sys
from selenium.webdriver.common.keys import Keys
from PIL import Image
from os import remove, listdir, sep
from os.path import basename, isfile
import tempfile
from warnings import warn
from baseclass import BaseClass

class backcall_is_robot(BaseClass):
    def test_backcall_is_robot(self):
        driver = self.driver
        test_texts = (u'Заявка на обратный звонок принята.',\
                     u'Оператор свяжется с Вами в течение',\
                     u'Ваш запрос на обратный звонок уже в очереди')
        rnumber = str(random.randrange(1000000000, 9999999999))
        end = 15
        for i in range(0, end):
            time.sleep(random.randrange(0, 3))
            #Link to callback
            inputElement = driver.find_element_by_css_selector("div.b-header-top-contacts-offline-callback")\
                           .find_element_by_css_selector("span.g-pseudo-h")
            inputElement.click()
            #Callback popup
            call_pop = self.wait_for_popub_name(u'Обратный звонок')
            #Send valid number
            inp_phone = call_pop.find_element_by_name("phone")
            inp_phone.clear()
            inp_phone.click()
            inp_phone.send_keys(rnumber)
            send_btn = call_pop.find_element_by_css_selector("input.b-sbutton-input")
            send_btn.click()
            #Check for ok popup
            if not self.wait_pop_with_any_text(test_texts):
                #Got an robot?
                if self.wait_pop_with_any_text((u'Точно не робот',)):
                    print 'Humans wins at '+str(i)+' try'
                    return True
            call_pop.find_element_by_css_selector("div.b-popup-close").click()
            #Check popup disappears
            WebDriverWait(driver, timeout=20).until(lambda dr: not self.check_exists(call_pop))
        self.fail('Robots wins')
